// Copyright 2024 Xiguan Inc
// Author: xiguan
// Email: xiguan.teng@qq.com
// Created on 2024/8/8
// This is ...
//

#ifndef MAPREDUCE_LAB1_LAB1_MASTER_H_
#define MAPREDUCE_LAB1_LAB1_MASTER_H_

#include <client.grpc.pb.h>
#include <client.pb.h>
#include <mapreduce.grpc.pb.h>
#include <mapreduce.pb.h>

class Master {
 public:
  Master() { work_status_.set_master_status(mapreduce::Status::WAITING); }
  void push_map_task(std::string &filename);

 public:
  mapreduce::WorkStatus                    work_status_;
  std::vector<std::string>                 map_task_;     // filename
  std::vector<std::pair<std::string, int>> reduce_task_;  // filename, id

  std::vector<std::pair<std::string, std::string>> prosessing_task_;
};

class MapReduceImpl final : public mapreduce::MapReduce::Service {
 public:
  MapReduceImpl(Master m) : master_(m) {}

  grpc::Status ask4task(grpc::ServerContext    *context,
                        const mapreduce::Empty *request,
                        mapreduce::WorkType    *response) override;

  void assign_work(mapreduce::WorkType *ans);

 private:
  Master master_;
};

class CountWordsImpl final : public mapreduce_client::CountWords::Service {
 public:
  CountWordsImpl(Master m) : master_(m) {}

  grpc::Status count_files_words(
      grpc::ServerContext                   *context,
      const mapreduce_client::ClientRequest *request,
      mapreduce_client::ClientReply         *response) override;

 private:
  Master master_;
};

#endif  // MAPREDUCE_LAB1_LAB1_MASTER_H_
